<template>
	<view>
		<cascader-select
			ref="picker" 
			idkey="value" 
			namekey="title" 
			childrenkey="children" 
			:title="$t('baseCommon.message.pleaseSelect', { title: $t('equipment.common.loca') })" 
			:isSuper="true" 
			:isLoad="true" 
			:initList="initList" 
			@load="loadNode" 
			@itemOnclick="itemOnclick"
			@itemOncancel="itemOncancel">
		</cascader-select>
	</view>
</template>

<script>
	import { getSubTreeList, getEqLocationInfo } from '@/api/base/equipment.js'
	import { notice } from '@/utils/common.js'
	
	export default {
		props: {
			locaId: {
				type: String
			}
		},
		data() {
			return {
				initList: []
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			async init() {
				let res = await this.getData(this.locaId)
				this.initList = res.data
				if(this.initList.length > 0) {
					this.$refs.picker._show()
				} else {
					notice.toast(this.$t('inspection.message.noLowerEquip'))
				}
			},
			async getData(locaId) {
				return await getSubTreeList(locaId)
			},
			async loadNode(e, resolve) {
				let res = await this.getData(e.value)
				resolve(res.data)
			},
			itemOnclick(e) {
				getEqLocationInfo(e.value).then(res => {
					if(res.data) {
						this.$emit('confirm', res.data)
					}
				})
			},
			itemOncancel() {
				this.$emit('confirm')
			}
		}
	}
</script>

<style>
</style>